package com.xiaoke.mapreduce.topn;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

//自定义类型必须实现接口： 序列化/反序列化   比较器
public class TKey implements WritableComparable<TKey> {

    private int year;
    private int month;
    private int day;
    //温度
    private int wd;

    private String location;


    @Override
    public int compareTo(TKey that) {
        // 可写可不写，如果重写了排序方法，则需要写，这里就作废
        int year = Integer.compare(this.year, that.getYear());
        if (year == 0) {
            int month = Integer.compare(this.month, that.getMonth());
            if (month == 0) {
                return Integer.compare(this.day, that.getDay());
            }
            return month;
        }
        return year;
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeInt(year);
        out.writeInt(month);
        out.writeInt(day);
        out.writeInt(wd);
        out.writeUTF(location);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.year = in.readInt();
        this.month = in.readInt();
        this.day = in.readInt();
        this.wd = in.readInt();
        this.location = in.readUTF();
    }


    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getWd() {
        return wd;
    }

    public void setWd(int wd) {
        this.wd = wd;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }
}
